package com.yisu.expressway.shopping_mall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.yisu.expressway.R;
import com.yisu.expressway.activity.BaseActivity;
import com.yisu.expressway.login.LoginPageActivity;
import com.yisu.expressway.model.LoginEvent;
import com.yisu.expressway.login.LoginManager;
import com.yisu.expressway.model.UserProfile;
import com.yisu.expressway.utils.StringUtil;
import com.youzan.sdk.YouzanSDK;
import com.youzan.sdk.YouzanUser;
import com.youzan.sdk.web.bridge.IBridgeEnv;
import com.youzan.sdk.web.event.UserInfoEvent;
import com.youzan.sdk.web.plugin.YouzanBrowser;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

public class YouZanWebActivity extends BaseActivity {

    @Bind(R.id.webview)
    protected YouzanBrowser mWebView;

    private String Url;
    private final static String GOODS_URL = "goods_url";
    private final static String WEB_TITLE = "title";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_you_zan_web);
        EventBus.getDefault().register(this);
        initWebView();
        initData(getIntent());
    }

    private void initData(Intent intent){
        Url = intent.getStringExtra(GOODS_URL);
        String title = intent.getStringExtra(WEB_TITLE);
        if(StringUtil.isNullOrEmpty(title)){
            title = getResources().getString(R.string.goods_detail);
        }
        setTitle(title);

        if(StringUtil.isNullOrEmpty(Url)){
            Url = "https://kdt.im/2e9wyr";
        }
        mWebView.loadUrl(Url);
    }

    private void initWebView(){
        mWebView.subscribe(new UserInfoEvent() {
            @Override
            public void call(IBridgeEnv iBridgeEnv) {
                //判断应用中用户是否登录
                if (LoginManager.isLoggedIn()) {
                    syncYzUser();
                } else {
                    startTargetActivity(LoginPageActivity.class);
                }
            }
        });
    }

    public static void launch(Context context,String url,String title){
        if(StringUtil.isNullOrEmpty(url)){
            return;
        }

        Intent intent = new Intent(context,YouZanWebActivity.class);
        intent.putExtra(GOODS_URL,url);
        intent.putExtra(WEB_TITLE,title);
        context.startActivity(intent);
    }

    @Subscribe
    public void onReceiverLoginEvent(LoginEvent event){
        if(event.getLoginResult()){
            syncYzUser();
        }else {
            onBackPressed();
        }
    }

    private void syncYzUser() {
        YouzanUser user = new YouzanUser();
        UserProfile userInfo = LoginManager.getUser();
        user.setUserId(userInfo.userAppId);
        if(!StringUtil.isNullOrEmpty(userInfo.userName)) {
            user.setNickName(userInfo.userName);//昵称, 会显示在有赞商家版后台
        }
        if(!StringUtil.isNullOrEmpty(userInfo.contactPhone)) {
            user.setTelephone(userInfo.contactPhone);
        }
        user.setUserName(userInfo.userAccount);//用户名
        YouzanSDK.syncRegisterUser(mWebView, user);

    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onBackPressed() {
        if (!mWebView.pageGoBack()) {
            super.onBackPressed();
        }
    }
}
